package com.example.qxfw.nyqx.controller;

import com.example.qxfw.common.SysResult;
import com.example.qxfw.common.entity.ny.Quhuatu;
import com.example.qxfw.common.entity.qx.MsgMediumSmallScale;
import com.example.qxfw.mongodb.hy.entity.HyImageFile;
import com.example.qxfw.nyqx.service.NyqhService;
import com.example.qxfw.nyqx.service.QuhuatuService;
import com.example.qxfw.ybzz.service.MsgMediumSmallScaleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;

@ApiIgnore
@Api(tags = "农业气象-农业区划")
@RestController
@CrossOrigin
@RequestMapping("/AgriculturalDivisionController")
public class AgriculturalDivisionController {

    @Autowired
    private QuhuatuService quhuatuService;

    @Autowired
    private MsgMediumSmallScaleService msgMediumSmallScaleService;

    @Autowired
    private NyqhService nyqhService;

    @ApiOperation("农业气象-农业区划-获取图层名称")
    @GetMapping("/getQuhuatuLayerName")
    public SysResult getQuhuatuLayerName(String qhClass){
        List<String> layerName = quhuatuService.getLayerName(qhClass);
        return SysResult.success(layerName);
    }

    @ApiOperation("农业气象-农业区划-获取区划图片")
    @GetMapping("/getQhImage")
    public SysResult getQhImage(String qhClassName, String qhType, HttpServletResponse response) throws IOException {
        byte[] imageData = nyqhService.getQuhuatu(qhClassName, qhType);
        if (imageData==null){
            return SysResult.fail("区划图不存在");
        }
        InputStream inputStream = new ByteArrayInputStream(imageData);

        response.setContentType("application/octet-stream");
        response.addHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(qhClassName+".jpg", "UTF-8"));
        ServletOutputStream outputStream = response.getOutputStream();
        byte[] b = new byte[1024];
        int len;
        while ((len = inputStream.read(b)) > 0) {
            outputStream.write(b, 0, len);
        }
        inputStream.close();
        return null;
    }

    @ApiOperation("农业气象-农业区划-保存区划图片")
    @PostMapping("/saveQhImage")
    public SysResult saveQhImage(MultipartFile qhImage,String qhClassName,String qhType) throws IOException {
        HyImageFile hyImageFile = nyqhService.saveQhImage(qhImage, qhClassName, qhType);
        return SysResult.success(hyImageFile);
    }

    @GetMapping("/getQuhuatu")
    public SysResult getQuhuatu(String qhClass,String layerType,String layerName){
        List<Quhuatu> list = quhuatuService.getList1(qhClass, layerType, layerName);
        return SysResult.success(list);
    }

    @GetMapping("/getQuhuatu1")
    public SysResult getQuhuatu1(String qhClass){
        List<Quhuatu> list = quhuatuService.getList1(qhClass);
        return SysResult.success(list);
    }

    @PostMapping("/updateQuhuatu")
    public SysResult updateQuhuatu(Quhuatu quhuatu){
        Integer rows = quhuatuService.update(quhuatu);
        return SysResult.success(rows);
    }

    @PostMapping("/saveQuhuatu")
    public SysResult saveQuhuatu(Quhuatu quhuatu){
        Integer rows = quhuatuService.save(quhuatu);
        return SysResult.success(rows);
    }

    @GetMapping("/getMsgMediumList")
    public SysResult getMsgMediumList(String date1,String date2,String dbName){
        List<MsgMediumSmallScale> list = msgMediumSmallScaleService.getList8(date1, date2, dbName);
        return SysResult.success(list);
    }

    @GetMapping("/getMsgMediumList1")
    public SysResult getMsgMediumList1(String date1,String date2,String dbName){
        List<MsgMediumSmallScale> list = msgMediumSmallScaleService.getList9(date1, date2, dbName);
        return SysResult.success(list);
    }




}
